import { NextFunction, Request, Response } from 'express'
import { errorRequestMiddleware } from './http-request'
import TimeoutError from '@/errors/TimeoutError'

// 超时处理中间件
export default (req: Request, res: Response, next: NextFunction) => {
  res.setTimeout(+(process.env.REQUEST_TIMEOUT || 5000), () => {
    const error = new TimeoutError()
    // 执行异常处理中间件
    errorRequestMiddleware(error, req, res, next)
  })
  next()
}
